package com.nantong.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.nantong.admin.annotation.PermissionRegister;
import com.nantong.admin.domain.Customer;
import com.nantong.admin.domain.model.request.customer.CustomerSearch;
import com.nantong.admin.domain.model.response.customer.CustomerVo;
import com.nantong.admin.service.ICustomerService;
import com.nantong.common.mybatis.core.page.PageQuery;
import com.nantong.common.mybatis.core.page.TableDataInfo;
import com.nantong.common.core.domain.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户Controller
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/system/customer")
@PermissionRegister(
    name = "客户管理",
    key = "system:customer",
    type = 2,  // 2=菜单
    path = "customer",
    component = "/views/Customer/CustomerView.vue",
    icon = "team",
    sortOrder = 2,
    parentKey = "system"  // 父菜单是system
)
@Tag(name = "客户管理", description = "客户管理相关接口")
public class CustomerController {

    private final ICustomerService customerService;

    /**
     * 获取客户列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取客户列表", description = "获取系统客户列表")
    @SaCheckPermission("system:customer:list")
    @PermissionRegister(
            name = "客户列表查询",
            key = "system:customer:list",
            type = 3,  // 3=按钮
            remark = "查询客户数据列表"
    )
    public R<List<CustomerVo>> list(CustomerSearch customerSearch) {
        List<CustomerVo> customers = customerService.searchList(customerSearch);
        return R.ok(customers);
    }

    /**
     * 获取客户分页
     */
    @SaCheckPermission("system:customer:page")
    @GetMapping("/page")
    @Operation(summary = "客户分页查询", description = "获取系统客户分页数据")
    @PermissionRegister(
            name = "客户分页查询",
            key = "system:customer:page",
            type = 3,  // 3=按钮
            remark = "查询客户数据分页"
    )
    public TableDataInfo<CustomerVo> page(CustomerSearch customerSearch, PageQuery pageQuery) {
        return customerService.selectPageList(customerSearch, pageQuery);
    }

    /**
     * 获取客户详细信息
     */
    @GetMapping("/{customerId}")
    @Operation(summary = "获取客户详情", description = "根据客户ID获取客户详情")
    @SaCheckPermission("system:customer:query")
    @PermissionRegister(
            name = "客户详情查询",
            key = "system:customer:query",
            type = 3,  // 3=按钮
            remark = "查询客户数据详情"
    )
    public R<Customer> getInfo(@PathVariable Long customerId) {
        Customer customer = customerService.getById(customerId);
        return R.ok(customer);
    }

    /**
     * 新增客户
     */
    @PostMapping
    @Operation(summary = "新增客户", description = "新增系统客户")
    @SaCheckPermission("system:customer:add")
    @PermissionRegister(
            name = "客户新增",
            key = "system:customer:add",
            type = 3,  // 3=按钮
            remark = "新增客户数据"
    )
    public R<Void> add(@RequestBody Customer customer) {
        customerService.save(customer);
        return R.ok();
    }

    /**
     * 修改客户
     */
    @PutMapping
    @Operation(summary = "修改客户", description = "修改系统客户")
    @SaCheckPermission("system:customer:edit")
    @PermissionRegister(
            name = "客户修改",
            key = "system:customer:edit",
            type = 3,  // 3=按钮
            remark = "修改客户数据"
    )
    public R<Void> edit(@RequestBody Customer customer) {
        customerService.updateById(customer);
        return R.ok();
    }

    /**
     * 删除客户
     */
    @DeleteMapping("/{customerId}")
    @Operation(summary = "删除客户", description = "删除系统客户")
    @SaCheckPermission("system:customer:delete")
    @PermissionRegister(
            name = "客户删除",
            key = "system:customer:delete",
            type = 3,  // 3=按钮
            remark = "删除客户数据"
    )
    public R<Void> remove(@PathVariable Long customerId) {
        customerService.removeById(customerId);
        return R.ok();
    }
}
